﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using Core.Avatar;
using Core.Features.FillIn;
using Core.Features.FillIn.ViewModel;
using Core.Features.MainMenu;
using Core.Util;

namespace Core.ViewModel
{
    internal class PhoneGameInitializer
    {
        private readonly Turtle _turtle;
        private readonly MainMenuDialog _menu;
        private readonly PhoneNumberGame _phone;
        private readonly ContentPresenter _dlgPresenter;

        public PhoneGameInitializer(Turtle turtle, MainMenuDialog menu, 
            PhoneNumberGame phone, ContentPresenter dlgPresenter)
        {
            _turtle = turtle;
            _menu = menu;
            _phone = phone;
            _dlgPresenter = dlgPresenter;

            _phone.Loaded += PhoneGameOnLoaded;
            _dlgPresenter.Content = _phone;
            _phone.FadeIn(721);
        }

            private void PhoneGameOnLoaded(object sender1, RoutedEventArgs routedEventArgs)
            {
                var vm = _phone.DataContext as PhoneNumberGameVM;
                vm.Incorrect += onVmOnIncorrect;
                vm.Correct += onVmOnCorrect;
                vm.Finished += onVmOnFinished;
            }

                private void onVmOnFinished(object o1, EventArgs args1)
                {
                    _dlgPresenter.Content = _menu;
                    _dlgPresenter.FadeIn(712);
                }

                private void onVmOnIncorrect(object o1, EventArgs args1)
                {
                    _turtle.Failure.Begin();
                }

                private void onVmOnCorrect(object o1, EventArgs args1)
                {
                    _turtle.Victory1.Completed += (sender2, eventArgs1) =>
                    {
                        _turtle.Victory1.Completed -= delegate { };
                        _turtle.HappyAnim.Begin();
                    };
                    _turtle.Victory1.Begin();
                }

    }
}
